package com.example.moblie2021606143cwj0511;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.apache.http.params.HttpConnectionParams;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class MainActivity extends AppCompatActivity {
    String urlString ="http://10.110.136.56:3000/posts/";


    private TextView tvMainConnect;
    private ImageView ivMainBanner;
    private TextView tvMainBan;
    final String IMAGEPATH = "";
    final String TEXTPATH = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL imageUrl = new URL(IMAGEPATH);
                    HttpsURLConnection imageConnection = (HttpsURLConnection) imageUrl.openConnection();
                    imageConnection.setRequestMethod("GET");
                    imageConnection.setConnectTimeout(5000);
                    InputStream imageIS = imageConnection.getInputStream();
                    final Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ivMainBanner.setImageBitmap(bitmap);
                        }
                    });
                    imageIS.close();
                    imageConnection.disconnect();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    URL textUri = new URL(TEXTPATH);
                    HttpsURLConnection textConnection = (HttpsURLConnection) textUri.openConnection();
                    InputStream textIS = textConnection.getInputStream();
                    InputStreamReader isr=new InputStreamReader(textIS);
                    final BufferedReader br= new BufferedReader(isr);
                    String line =null;
                    String temp="";
                    while ((line=br.readLine())!=null){
                        temp+=line;
                        temp += "\n";
                    }
                    final String msg =temp;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvMainConnect.setText(msg);
                        }
                    });
                    br.close();
                    isr.close();
                    textIS.close();
                    textConnection.disconnect();
                }catch (Exception e){

                }
            }
        }.start();
    }

    private void initView() {
        tvMainConnect = (TextView) findViewById(R.id.tv_main_connect);
        ivMainBanner = (ImageView) findViewById(R.id.iv_main_banner);
        tvMainBan = (TextView) findViewById(R.id.tv_main_ban);
    }
}